//
//  SetNewPasswordController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/30.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "SetNewPasswordController.h"

@interface SetNewPasswordController ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIProgressView *progress;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@property (nonatomic , strong) HKPasswordTF * tf;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;

@property (nonatomic , strong) NSString * firstPassword;
@property (nonatomic , strong) NSString * secondPassword;
@property (nonatomic , assign) NSInteger times;

@end

@implementation SetNewPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)setNavi
{
    self.title = @"修改支付密码";
}

-(void)setUI
{
    self.view.backgroundColor =  Color(245, 245, 245);
    self.times = 0;
    self.typeLabel.text = self.labelText;
    
    [self.progress setProgress:1 animated:YES];
    self.tf = [[HKPasswordTF alloc]init];
    self.tf.elementCount = 6;
    self.tf.elementMargin = 6;
    self.tf.elementBorderColor = TextLessImportColor;
    [self.bgView addSubview:self.tf];
    [self.tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.top.equalTo(self.label.mas_bottom).offset(15); 
        make.bottom.offset(-15);
    }];
    
    __weak typeof(self) weakSelf = self;
    self.tf.passwordDidChangeBlock = ^(NSString *password) {
        if (password.length == 6 && self.times == 0) {
            weakSelf.nextBtn.userInteractionEnabled = YES;
            [weakSelf.nextBtn setBackgroundColor:MainColor];
            weakSelf.firstPassword = password;
            [weakSelf sendFirstPassword:password];
        }else if (self.times == 1 && password.length == 6 && [self.firstPassword isEqualToString:password]){
            weakSelf.times = 2;
            [weakSelf.nextBtn setBackgroundColor:MainColor];
            weakSelf.nextBtn.userInteractionEnabled = YES;
        }else if(self.times == 1 && password.length == 6 && ![self.firstPassword isEqualToString:password])
        {
            [MBProgressHUD showError:@"两次密码输入不一致"];
            [weakSelf.tf clearPassword];
        }else
        {
            weakSelf.nextBtn.userInteractionEnabled = NO;
            weakSelf.nextBtn.backgroundColor = MainBtnColor;
        }
    };
}

- (void)sendFirstPassword:(NSString *)password
{
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
    NSDictionary * dict = @{@"token":weakSelf.userToken,@"first_password":password};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/update-payment-password",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (isSuccess) {
            [weakSelf.nextBtn setTitle:@"确定" forState:(UIControlStateNormal)];
            weakSelf.nextBtn.userInteractionEnabled = NO;
            weakSelf.times = 1;
            weakSelf.label.text = @"请再次输入支付密码";
            [weakSelf.tf clearPassword];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    }];
}

- (IBAction)againAction:(UIButton *)sender {
    
    if (self.times == 2) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary * dict = @{@"token":self.userToken,@"second_password":self.firstPassword};
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/update-payment-password",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (isSuccess) {
                [MBProgressHUD showSuccess:@"设置完成"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else if (self.times == 1)
    {
        [self sendFirstPassword:self.firstPassword];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
